package ${basePackage}.common;

import lombok.Data;

import java.util.HashMap;

@Data
public class QueryParam extends HashMap<String, Object> {

    public String getString(String key) {
        Object value = get(key);
        if (value == null) {
            return null;
        }
        return value.toString();
    }

    public Boolean getBoolean(String key) {
        Object value = get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Boolean) {
            return (Boolean) value;
        }
        if (value instanceof String) {
            return Boolean.valueOf((String) value);
        }
        throw new IllegalArgumentException("Can not convert Boolean type, key=" + key + ", value=" + value);
    }
    
    public Integer getInteger(String key) {
        Object value = get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Integer) {
            return (Integer) value;
        }
        if (value instanceof Long) {
            return ((Long) value).intValue();
        }
        if (value instanceof String) {
            return Integer.valueOf((String) value);
        }
        throw new IllegalArgumentException("Can not convert Integer type, key=" + key + ", value=" + value);
    }

    public Long getLong(String key) {
        Object value = get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Integer) {
            return ((Integer) value).longValue();
        }
        if (value instanceof Long) {
            return (Long) value;
        }
        if (value instanceof String) {
            return Long.valueOf((String) value);
        }
        throw new IllegalArgumentException("Can not convert Long type, key=" + key + ", value=" + value);
    }

    public Double getDouble(String key) {
        Object value = get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Float) {
            return ((Float) value).doubleValue();
        }
        if (value instanceof Double) {
            return (Double) value;
        }
        if (value instanceof String) {
            return Double.valueOf((String) value);
        }
        throw new IllegalArgumentException("Can not convert Double type, key=" + key + ", value=" + value);
    }

    public Float getFloat(String key) {
        Object value = get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Double) {
            return ((Double) value).floatValue();
        }
        if (value instanceof Float) {
            return (Float) value;
        }
        if (value instanceof String) {
            return Float.valueOf((String) value);
        }
        throw new IllegalArgumentException("Can not convert Float type, key=" + key + ", value=" + value);
    }
}
